//
//  ZFNetRequest.swift
//  map
//
//  Created by fyzq on 2024/2/5.
//

import Foundation
import Moya
import RxSwift
import HandyJSON
enum ZFRequestApi {
    case list(pageNO: Int = 1, pageSize: Int = 10)
    case data(params:[String:Any])
    case geojson(code: String)
    case groupList
}
/// 实现TargetType协议
extension ZFRequestApi: TargetType {
    var baseURL: URL {
        switch self {
        case .geojson:
            return URL(string: "https://geo.datav.aliyun.com/areas_v3/bound/geojson")!
        default:
            return URL(string: "https://map.kky888.cn")!
        }
        
    }
    
    var path: String {
        switch self {
        case .geojson:
            return ""
        case .groupList:
            return "/videoManage/api/groupList"
        default:
            return ""
        }
    }
    
    /// 请求方式
    var method: Moya.Method {
        switch self {
        
        case .groupList ,.geojson :
            return .get
            
        default:
            return .post
        }
    }
    
    /// 解析格式
    var sampleData: Data {
        return "{}".data(using: String.Encoding.utf8)!
    }
    
    
    
    var task: Moya.Task {
        var params: [String: Any] = [:]
        switch self {
        case let .list(pageNO, pageSize):
            params["pageNO"] = pageNO
            params["pageSize"] = pageSize
            break
        case .data(let data):
            params.merge(data) { (_, newValue) in return newValue }
            break
        case .geojson(let code):
            params["code"] = code
            break
        case .groupList: break
            
        }
        
        // 发起请求
        return .requestParameters(parameters: params, encoding: URLEncoding.default)
    }
    
    var headers: [String : String]? {
        return ["Content-Type":"application/json","deviceId":String.UUID(),"Authorization":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwaG9uZSI6IjE1MTUxODc2NDg3IiwiaWQiOjEwMzU1MTI3MzgxODY1MjY3MjAsImV4cCI6MTcxMDEwMzQ5NCwiZGV2aWNlSWQiOiIwQkY1M0E4Qy1CMDNFLTQxREUtQjRCRi1DNEVGQzRGRENDOTUifQ.DXofWTsDrVimIpMWLdG9kE6oiBivHJY2HpYlhpQufTw"]
    }
    
    
}
class LoadingPlugin: PluginType {
    
}


class ZFNetRequest {
    private static var provider = MoyaProvider<ZFRequestApi>()

    static func request(token:ZFRequestApi)->Observable<ZFBaseModal>{
        return Observable.create { observer in
            return provider.rx.request(token).asObservable().subscribe(onNext: {respone in
                if respone.statusCode == 200 {
                    guard let json = try? respone.mapJSON(),
                          let modal = ZFBaseModal.deserialize(from: json as? [String:Any]) else {
                        observer.onError(NSError(domain: "错误", code: 1))
                        return
                    }
                    observer.onNext(modal)
                }else {
                    observer.onError(NSError(domain: "服务器错误", code: respone.statusCode))
                }
                observer.onCompleted()
            })
        }
    }
    
    
    static func geojson(code:String)->Observable<Data>{
        return Observable.create { observer in
            return provider.rx.request(ZFRequestApi.geojson(code: code)).asObservable().subscribe(onNext: {respone in
                if respone.statusCode == 200 {
                    observer.onNext(respone.data)
                }else {
                    observer.onError(NSError(domain: "", code: 201))
                }
                observer.onCompleted()
            })
        }
    }
    
}
